MDBF QAT - #42
Open
fujisawa-yoshihiko wants to merge 3 commits into
Open
Conversation
Introduce the piece needed to make MultipathMDBFLinear layers trainable during GlobalPTQ-style QAT: - mdbf_adapter.py: promotes per-path amplitude parameters (A_amp, B_amp, Q_U_amp, Q_V_amp) to fp32 nn.Parameter and, when optimize_binary=True, exposes +/-1 sign matrices as float shadow tensors trained through a smooth sign STE. Provides differentiable forward reconstruction, write-back to packed/fp16 buffers for inference, and state snapshot/restore for rollback. This module is not wired into the training loop yet; see the following commit for integration into GlobalPTQ/GlobalPTQDistributed. Co-authored-by: Cursor <cursoragent@cursor.com>
- helpers.detect_quantization_method: recognize MultipathMDBFLinear layers as method "mdbf" (priority gptq > dbf > mdbf). Without this a MDBF-quantized model was reported as having no quantized layers and the distillation loop skipped it entirely. - core.run_kl_distillation: add the "mdbf" branch (setup/teardown via mdbf_adapter, param groups for amplitude/binary params using dbf_lr) and expose mdbf_ste_k for the sign STE sharpness. - core.eval_kl / trainer._GlobalPTQTrainer.compute_loss: support a separate teacher_device so the FP16 teacher can be kept on CPU or a second GPU while the student trains under DeepSpeed ZeRO-2 with CPU optimizer offload. Required to fit 1bpw Llama2-7B/13B QAT on 80GB cards. - global_ptq.py / global_ptq_distributed.py: expose the corresponding GlobalPTQConfig(Distributed) fields (mdbf_ste_k, student_device, teacher_device) and thread them through to run_kl_distillation and the Trainer-based distributed path. Co-authored-by: Cursor <cursoragent@cursor.com>
detect_quantization_method() imported MultipathMDBFLinear unconditionally, so on an installation without onecomp.quantizer.mdbf *every* call raised ModuleNotFoundError -- including for plain GPTQ and DBF models. On this PR's base branch that broke the existing TestDetectQuantizationMethod tests (3 failures). Guard the import and treat MDBF as absent when it cannot be imported, so GPTQ/DBF detection keeps working without the MDBF quantizer. Verified both ways: without MDBF the global_ptq unit tests pass (69 passed, integration tests deselected), and with MDBF present (merged with feature/mdbf) detection still resolves MultipathMDBFLinear.
fujisawa-yoshihiko
force-pushed
the
feature/mdbf-qat-globalptq
branch
from
July 29, 2026 13:57
75fec39 to
85896e2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
onecomp-globalptqの KL 蒸留パイプラインに MDBF 量子化モデルの QAT 経路を追加します。MDBF の振幅パラメータと ±1 符号行列を smooth sign STE で同時に学習できるようになります。ライブラリ本体のみの変更で、既存の GPTQ / DBF 経路の挙動は変えていません。
1. MDBF 微分可能アダプタ (
b8569af)背景・動機
global_ptqの KL 蒸留ループは、量子化方式ごとのアダプタが「学習対象パラメータの取り出し → 微分可能 forward への差し替え → 学習後の書き戻し」を担う設計になっています。MDBF は重みをと ±1 符号行列と多段振幅の積で表すため、既存の GPTQ / DBF 用アダプタでは学習対象を取り出せません。MDBF 専用のアダプタが必要でした。
変更内容(新規
_core/mdbf_adapter.py、+319)MultipathMDBFLinearの forward を微分可能な実装に差し替え、パスごとの振幅パラメータ(A_amp,B_amp,Q_U_amp,Q_V_amp)を fp32 のnn.Parameterとして最適化対象に昇格optimize_binary=Trueのとき、符号行列A_sign/B_signを float として露出し、smooth sign STE(mdbf_ste_k、既定2.0)で学習setup_mdbf_differentiable()/restore_mdbf_original()で元の forward へ復帰可能write_back_mdbf_amp()/write_back_mdbf_binary()(packed / fp16 バッファへ)、ロールバック用の状態保存・復元はsave_mdbf_state()/load_mdbf_state()2. QATへの接続 (
b9940cc)背景・動機
3 点あります。
detect_quantization_method()で量子化方式を自動判別しますが、MDBF が候補に無いため、MDBF 量子化済みモデルを渡しても「量子化層なし」として素通りしていましたkは論文値に近づけるための主要な探索軸のため、設定として露出させる必要がありました(今回の実測はk=2.0、次段でk=100を試す予定)変更内容
_core/helpers.py:detect_quantization_method()がMultipathMDBFLinearを"mdbf"として検出。優先度は GPTQ > DBF > MDBF で、混在時は警告を出して最優先のもののみ返します(従来の GPTQ / DBF の挙動は不変)_core/core.py:run_kl_distillation()に mdbf 分岐を追加(setup / teardown は上記アダプタ経由、振幅・符号のパラメータ群はdbf_lrを使用)。eval_kl()がteacher_deviceを生徒と分離して受け取れるように変更_core/trainer.py:_GlobalPTQTrainer.compute_loss()も同様に教師デバイスの分離に対応global_ptq.py/global_ptq_distributed.py:mdbf_ste_k/student_device/teacher_deviceを設定として追加し、run_kl_distillationおよび Trainer ベースの分散経路へ引き渡し依存関係についてのご注意
本 PR のコードは
onecomp/quantizer/mdbf/(MDBF 量子化器本体)に依存しますが、このブランチには MDBF 本体を含めていません。MDBF は既に #28 でfeature/mdbfにマージ済みのため、そちらとの統合をお願いしたく思います。パッケージが MDBF 非搭載でも壊れないよう、
global_ptq内の MDBF 参照はすべて関数内の遅延 import にしてあります。onecomp_globalptqの import、および既存の gptq / dbf 経路は MDBF 非搭載でもそのまま動作します(動作確認済み)ImportErrorになりますfeature/mdbf へマージする場合の解決手順(コンフリクト 3 ファイル)
export/global_ptqとfeature/mdbfの分岐に由来するもので、本 PR の 2 コミットが原因ではありません。衝突するのはCHANGELOG.md/onecomp/quantizer/__init__.py/onecomp/quantized_model_loader.pyの 3 ファイルで、いずれも「MDBF 側の追加」と「OneBit 側の追加」が並列に入っただけなので、両方を残すのが正しい解決です。CHANGELOG.md: 両方のエントリを残すonecomp/quantizer/__init__.py:feature/mdbf側(HEAD)が上位集合なので HEAD を採用onecomp/quantized_model_loader.pyは 6 ハンクありますが、最後の 2 ハンクはelifの分岐が共通の末尾(from_saved_state(...)の引数列)を共有しているため、機械的に両側を連結するとSyntaxErrorになります。両分岐をそれぞれ完全な形で書き出してください。残りの 3 ハンクは以下のとおりです。
マージ後の確認:
変更ファイル
global_ptq/…/_core/mdbf_adapter.pyglobal_ptq/…/_core/core.pyteacher_deviceの分離global_ptq/…/_core/helpers.pydetect_quantization_methodの MDBF 対応global_ptq/…/_core/trainer.pyglobal_ptq/…/global_ptq.pymdbf_ste_k/student_device/teacher_deviceの追加global_ptq/…/global_ptq_distributed.pymdbf_ste_k/teacher_deviceの追加と分散経路への引き渡し検証
本 PR の QAT 経路を用いて、Llama2-7B / 13B で MDBF 論文プロトコル(1.00 BPW)を実行し、PTQ・QAT とも完走することを確認しています。
実行環境: TSUBAME4 node_h, H100 80GB × 2(QAT 時 GPU0 ≈52.5GB / GPU1 ≈28GB(FP16 教師))、DeepSpeed ZeRO-2
設定:
target_bits=1.0,P=1, ADMMiters=1000/inner=3/reg=0.03, gradient refineiters=1500/lr=0.01,activation_aware=True,act_init="osvd", QEPpercdamp=0.01/perccorr=0.5, 先頭 4 + 末尾 4 ブロックを FP16 除外, calibration = C4 512 サンプル × 2048 トークン。Table 1 はl=8の PTQ のみ、Table 2 はl=3+ QAT(KL 蒸留のみw_distill=1.0/w_ntp=0.0,optimize_binary=True,epochs=5,dbf_lr=5e-5,mdbf_ste_k=2.0,calibration_strategy="drop_rand")。注記: これらを実行した論文プロトコル再現スクリプトと、そのために必要だった評価用データセット読み込みの修正(
onecomp/utils/perplexity.pyほか)は、本 PR のスコープ外としています。必要でしたら別 PR で提出します。Test plan
onecomp_globalptqが import でき、既存の GPTQ / DBF 経路が従来どおり動作することdetect_quantization_method()が MDBF モデルで"mdbf"を返し、GPTQ 混在時は"gptq"を優先することfeature/mdbfとマージした状態で MDBF QAT(optimize_binary=True)が完走することteacher_deviceを CPU / 別 GPU に指定して KL 蒸留が動作することwrite_back_mdbf_amp()/write_back_mdbf_binary()後の量子化モデルが保存・再読み込みできること既知の制限・今後
kを 2.0 → 100 → 中間層蒸留の順で追試予定ですuse_inter_loss/lambda_inter)はGlobalPTQにのみ実装されており、GlobalPTQDistributed側には未接続ですglobal_ptq/配下の既存ファイルは base の時点で black / isort 未適用のため、本 PR でも整形はかけていません(無関係な差分を増やさないため)。新規追加したmdbf_adapter.pyは black(--line-length=99)/ isort 適用済みですスコープ外(別途提出を検討)
onecomp/quantizer/mdbf/)— feature/mdbf: gemlite対応とHessian/scale_bits修正 #28 でfeature/mdbfにマージ済みcalibration_datasetに事前ロード済みテキスト(list[str])を渡せるようにする拡張